home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / UTILITIE / CPU_MEMO / 3468.ZIP / POPUP.ZIP / WINDOWS.C < prev    next >
C/C++ Source or Header  |  1988-06-30  |  9KB  |  267 lines

  1. /*****************************************************************************
  2. * Module       : WINDOWS.C                                                   *
  3. *----------------------------------------------------------------------------*
  4. * Program      : SCREEN.LIB                                                  *
  5. *----------------------------------------------------------------------------*
  6. * Author       : M.R.Watson.    Copyright (c) M.R.Watson 1987                *
  7. *----------------------------------------------------------------------------*
  8. * Last Updated : 17/06/88                                                    *
  9. *----------------------------------------------------------------------------*
  10. *                                 Synopsis                                   *
  11. *                                 ========                                   *
  12. * A "mini-window" system designed specifically for use in programs which     *
  13. * must be as small as possible. The screen and window handling is very       *
  14. * basic at the moment, and thus takes up little room. It is envisioned that  *
  15. * popup programs would use this module.                                      *
  16. *----------------------------------------------------------------------------*
  17. * Note : Before using any functions in this module, you must first call the  *
  18. * "monitor()" function, which sets up the screen RAM start etc etc.          *
  19. *****************************************************************************/
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <malloc.h>
  24. #include <dos.h>
  25. #include <standard.h>
  26.  
  27.  
  28. /* These next two are used in the SCREENIO.ASM module. */
  29.  
  30. WORD    DisplaySegment, DisplayStatus ;
  31.  
  32.  
  33. /*****************************************************************************
  34. * BYTE *SaveBox( BYTE *ptr, int top, lft, bot, rgt )                         *
  35. *----------------------------------------------------------------------------*
  36. * If "p" is not NULL, it is used instead of the malloced area mentioned next.*
  37. * Mallocs an area of memory large enough to hold the area of screen given,   *
  38. * copies the area of screen into it, and returns a pointer to it.            *
  39. *----------------------------------------------------------------------------*
  40. * Returns a pointer to the saved area of screen.                             *
  41. * Returns NULL if bad arguments or out of memory.                            *
  42. *****************************************************************************/
  43.  
  44. BYTE *
  45. SaveBox( ptr, top, lft, bot, rgt )
  46. BYTE *ptr ;
  47. {
  48.     int     len1, len2, i   ;
  49.     BYTE    *p, *q          ;
  50.  
  51.     if (DisplaySegment == 0)
  52.     {
  53.         printf("You have not called the monitor() function!\n");
  54.         exit(FAIL);
  55.     }
  56.  
  57.     if ( bot < top  ||  rgt < lft )
  58.         return(NULL);
  59.  
  60.     len1 = rgt - lft + 1 ;
  61.     len2 = len1 * 2 ;
  62.  
  63.     if (ptr)
  64.         p = q = ptr ;
  65.     else if ( ( q = p = malloc( len2 * ( top - bot + 1 ) ) ) == NULL )
  66.         return(NULL);
  67.  
  68.     for ( i = top  ;  i <= bot  ;  i++, p += len2 )
  69.         ReadScreen( i, lft, p, len1 );
  70.  
  71.     return(q);
  72. }
  73.  
  74.  
  75. /*****************************************************************************
  76. * RestoreBox( BYTE *ptr, int top, lft, bot, rgt )                            *
  77. *----------------------------------------------------------------------------*
  78. * Given a saved area of screen (saved by SaveBox), pointed to by "ptr",      *
  79. * restores the saved area to the given area bounded by top,lft,bot,rgt.      *
  80. *****************************************************************************/
  81.  
  82. RestoreBox( ptr, top, lft, bot, rgt )
  83. BYTE    *ptr    ;
  84. {
  85.     int     len1, len2, i   ;
  86.  
  87.     if ( bot < top  ||  rgt < lft  ||  ptr == NULL )
  88.         return(ERROR);
  89.  
  90.     len1 = rgt - lft + 1 ;
  91.     len2 = len1 * 2 ;
  92.  
  93.     for ( i = top  ;  i <= bot  ;  i++, ptr += len2 )
  94.         WriteScreen( i, lft, ptr, len1 );
  95.  
  96.     return(SUCCEED);
  97. }
  98.  
  99. /*****************************************************************************
  100. * ClearBox( int top, lft, bot, rgt, colour )                                 *
  101. *----------------------------------------------------------------------------*
  102. * Clears the given area to the given colour, using spaces.                   *
  103. * (Thus to get a colour other that black, use a non-black background colour) *
  104. *****************************************************************************/
  105.  
  106. ClearBox( top, lft, bot, rgt, colour )
  107. {
  108.     WORD        buf[80] ;
  109.     WORD        x       ;
  110.     register    i, len  ;
  111.  
  112.     if ( bot < top  ||  rgt < lft  ||  colour < 0 )
  113.         return(ERROR);
  114.  
  115.     len = min( rgt - lft + 1, 80 );
  116.     x = ( (BYTE)colour << 8 ) + (BYTE)' ' ;
  117.  
  118.     for ( i = 0  ;  i < len  ;  i++ )
  119.         buf[i] = x ;
  120.  
  121.     for ( i = top  ;  i <= bot  ;  i++ )
  122.         WriteScreen( i, lft, buf, len );
  123. }
  124.     
  125.  
  126. /*****************************************************************************
  127. * border( int top, lft, bot, rgt, char *title, *note, int colour )           *
  128. *----------------------------------------------------------------------------*
  129. * Draws a nice border around the given area of screen.                       *
  130. * The border is drawn with spaces. The border and title are given the        *
  131. * attribute "colour". This should be something like black on <some colour>.  *
  132. *****************************************************************************/
  133.  
  134. border( top, lft, bot, rgt, title, note, colour )
  135. char    *title, *note   ;
  136. {
  137.     WORD        buf[80] ;
  138.     register    i, len  ;
  139.     WORD        x       ;
  140.  
  141.     if ( bot < top  ||  rgt < lft  ||  colour < 0 )
  142.         return(ERROR);
  143.  
  144.     len = min( rgt - lft + 1, 80 );
  145.     x = ( (BYTE)colour << 8 ) + (BYTE)' ' ;
  146.  
  147.     for ( i = 0  ;  i < len  ;  i++ )
  148.         buf[i] = x ;
  149.  
  150.     WriteScreen( top, lft, buf, len );
  151.     WriteScreen( bot, lft, buf, len );
  152.  
  153.     for ( i = top  ;  i <= bot  ;  i++ )
  154.     {
  155.         WriteScreen( i, lft, buf, 1 );
  156.         WriteScreen( i, rgt, buf, 1 );
  157.     }
  158.  
  159.     if (title)
  160.         PrintCentered( top, lft, rgt, title, colour );
  161.  
  162.     if (note)
  163.         PrintCentered( bot, lft, rgt, note, colour );
  164.  
  165.     return(SUCCEED);
  166. }
  167.  
  168.  
  169. /*****************************************************************************
  170. * PrintCentered( int row, lft, rgt, char *text, int colour )                 *
  171. *----------------------------------------------------------------------------*
  172. * Writes the given text in the given colour centrally on the given row.      *
  173. *****************************************************************************/
  174.  
  175. PrintCentered( row, lft, rgt, text, colour )
  176. char    *text   ;
  177. {
  178.     int     len1, len2  ;
  179.  
  180.     if ( rgt < lft  ||  colour < 0  ||  text == NULL )
  181.         return(ERROR);
  182.  
  183.     len1 = strlen(text);
  184.     len2 = rgt - lft + 1 ;
  185.  
  186.     if ( len2 < len1 )
  187.         return(ERROR);
  188.  
  189.     prn( row, lft + (len2-len1)/2, text, colour );
  190.     return(SUCCEED);
  191. }
  192.  
  193.  
  194. /*****************************************************************************
  195. * prn( int row, int col, char *text, int colour )                            *
  196. *----------------------------------------------------------------------------*
  197. * Prints the specified text at the given position in the required colour.    *
  198. *****************************************************************************/
  199.  
  200. prn( row, col, text, colour )
  201. char    *text   ;
  202. {
  203.     WORD            buf[80] ;
  204.     WORD            c       ;
  205.     int             n, i    ;
  206.     register BYTE   *p      ;
  207.     register WORD   *q      ;
  208.  
  209.     if ( text == NULL  ||  colour < 0 )
  210.         return(ERROR);
  211.  
  212.     c = (WORD)colour << 8 ;
  213.     n = min( strlen(text), 80 );
  214.  
  215.     for ( p = text, q = buf, i = 0  ;  i < n ;  i++, p++, q++ )
  216.         *q = *p + c ;
  217.         
  218.     WriteScreen( row, col, buf, n );
  219.     return(SUCCEED);
  220. }
  221.  
  222.  
  223. /*****************************************************************************
  224. * monitor(void) : Get monitor type, set display segment & status port        *
  225. *----------------------------------------------------------------------------*
  226. * Returns 0 for mono, 1 for CGA, 2 for EGA mono, 3 for EGA colour monitor.   *
  227. *****************************************************************************/
  228.  
  229. monitor()
  230. {
  231.     union REGS  regs        ;
  232.  
  233.     int86( 0x11, ®s, ®s );
  234.  
  235.     switch( regs.x.ax & 0x30 )
  236.     {
  237.         case 48 :
  238.  
  239.             DisplaySegment = 0xB000 ;
  240.             DisplayStatus  = 0x03BA ;
  241.             return(0);                      /* mono monitor */
  242.  
  243.         case 16 :
  244.         case 32 :
  245.  
  246.             DisplaySegment = 0xB800 ;
  247.             DisplayStatus  = 0x03DA ;
  248.             return(1);                      /* color monitor */
  249.  
  250.         case 00 :                           /* ega card... */
  251.  
  252.             if ( peekb( 0x40, 0x87 ) & 1 )  /* ...in mono mode */
  253.             {
  254.                 DisplaySegment = 0xB000 ;
  255.                 DisplayStatus  = 0x03BA ;
  256.                 return(2);
  257.             }
  258.             else                            /* ...in colour mode */
  259.             {
  260.                 DisplaySegment = 0xB800 ;
  261.                 DisplayStatus  = 0x03DA ;
  262.                 return(3);
  263.             }
  264.     }
  265. }
  266.  
  267.